// pages/mine/userInfoEdit.js
const app = getApp();
Page({

  data: {
    form: {},
  },

  onLoad: function () {

  },
  onShow() {
    this.getUser()
  },
  async getUser() {
    const res = await app.request({ url: `getUser`, data: { openid: app.openid }, method: `POST` })
    if(res.meta.status>=300)return app.showModal({content:`获取用户资料失败:`+res.meta.msg})
    const user = res.data[0]
    this.setData({
      form:user
    })
  },
  onInput(e) {
    const { key } = e.target.dataset
    const { value } = e.detail
    const { form } = this.data
    form[key] = value
    this.setData({
      form
    })
  },
  bindPickerChange(e) {
    console.log(e);
    const { key, array } = e.target.dataset
    const { value: index } = e.detail
    const { form, helper } = this.data
    const keyArrayName = key + 'Array'
    const keyIndexName = key + 'Index'
    const value = helper[keyArrayName][index]
    form[key] = value
    helper[keyIndexName] = index
    this.setData({
      form,
      helper
    })
  },
  bindMultiPickerChange(e) {
    console.log(e);
    const { key } = e.target.dataset
    let { value } = e.detail
    const { form } = this.data
    value = value.toString()
    form[key] = value
    this.setData({
      form,
    })
  },
  onClickTipsAccount() {
    app.showModal({ content: `示例：支付宝+手机号 或者 微信+手机号 或者 银行卡号` })
  },
  async onSubmit() {
    const { form } = this.data
    if (!form.name || form.name.trim() === '') return app.showModal({ content: '请完善 姓名' })
    if (!form.phone || form.phone.trim() === '') return app.showModal({ content: '请完善 电话' })
    if (!form.account || form.account.trim() === '') return app.showModal({ content: '请完善 收款账户' })
    if (!form.provinces || form.provinces.trim() === '') return app.showModal({ content: '请完善 收货地区' })
    if (!form.address || form.address.trim() === '') return app.showModal({ content: '请完善 详细地址' })
    const res = await app.request({ url: "updateUser", data: { ...form, openid: app.openid }, method: "POST" })
    if (res.meta.status >= 300) return app.showModal({ content: '上传用户信息失败' + res.meta.msg })
    app.showModal({ content: '保存成功' })
    // wx.navigateTo({ url: '/pages/mine/certification/certification' });
  },
})
